/* * * * M A P V I S I O N   L I C E N S E  *****************************************************************
 *
 * This MapVision software is © Filip Zavadil 2013. It is licensed under the LGPL license.
 * This MapVision software dynamically links to unmodified Qt5 Library.
 *
 * The Qt5 Library is © 2013 Qt® and/or its subsidiary(-ies), and is licensed under the
 * GNU Lesser General Public License version 2.1 with Qt® LGPL exception version 1.1.
 * Qt5 library is free software; you can redistribute it and/or modify it under the terms of the
 * GNU Lesser General Public License, version 2.1, as published by the Free Software Foundation.
 * Qt5 library is provided “AS IS”, without WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR
 * IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
 *
 * As an additional permission to the GNU Lesser General Public License version 2.1, the object
 * code form of a “work that uses the Library” may incorporate material from a header file that is
 * part of the Library. You may distribute such object code under terms of your choice,
 * provided that: (i) the header files of the Library have not been modified; and (ii)
 * the incorporated material is limited to numerical parameters, data structure layouts, accessors,
 * macros, inline functions and templates; and (iii) you comply with the terms of Section 6 of the
 * GNU Lesser General Public License version 2.1.
 *
 * Moreover, you may apply this exception to a modified version of the Library, provided that such
 * modification does not involve copying material from the Library into the modified Library’s header
 * files unless such material is limited to (i) numerical parameters; (ii) data structure layouts;
 * (iii) accessors; and (iv) small macros, templates and inline functions of five lines or less in length.
 *
 * Furthermore, you are not required to apply this additional permission to a modified version of the Library.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this package;
 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

 * The source code for Qt 5.X SDK is available from Qt Project here:
 * http://download.qt-project.org/official_releases/qtcreator/2.7/2.7.0/qt-creator-2.7.0-src.zip.
 *
 * ****************************** M A P V I S I O N    S O F T W A R E  ************************************
 *
 * Author:       Filip Zavadil
 * Description:  Software for displaying and exporting map tiles.
 * The sources and executables of MapVision software are available:
 *    - from project hosting on CodePlex.com :   http://mapvision.codeplex.com
 *    - from GIT repositories on Gitorious.org:  https://gitorious.org/mapvision
 * Visit MapVision fanpage on Facebook.com:      https://www.facebook.com/pages/Mapvision/631827753513935
 *
 * *********************************************************************************************************
*/

#include "graphicsfeatureitemsvg.h"
#include <QPainter>
#include <QDebug>
#include <QMenu>
#include "slideraction.h"
#include "spinboxaction.h"
#include "spinBoxAction.h"
#include "checkboxaction.h"

GraphicsFeatureItemSVG::GraphicsFeatureItemSVG(DockWidgetAddMapFeatures *parent, QString filename, double scale, double opacity) : QGraphicsSvgItem(filename)
{
    this->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable | ItemSendsGeometryChanges);
    this->setZValue(999);
    this->setTransformOriginPoint(this->boundingRect().center());
    this->setScale(scale);
    this->setOpacity(opacity);

    theDock = parent;

   mapView = parent->getMapView();

    connect(mapView,SIGNAL(zoomChanged(int)),this,SLOT(zoomHasChanged(int)));
    zoom = mapView->getZoom();

    QPoint pos = zavaProj.lonlat2pixels(mapView->getCenterLonLat(),mapView->getZoom());
    this->moveBy(pos.x()-this->boundingRect().center().x(),pos.y()-this->boundingRect().center().y());
    lastPixels = this->pos();
}

void GraphicsFeatureItemSVG::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    QGraphicsSvgItem::paint(painter, option, widget);
}

void GraphicsFeatureItemSVG::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
{
    QMenu menu;
    ;
    QAction * closeAction = new QAction(tr("remove"),this);
    connect(closeAction,SIGNAL(triggered()),this,SLOT(deleteItem()));
    SliderAction * sA = new SliderAction(tr("opacity"));
    sA->getSlider()->setValue(opacity()*100);
    connect(sA->getSlider(),SIGNAL(valueChanged(int)),this,SLOT(transChanged(int)));

    SPinBoxAction * sbA = new SPinBoxAction(tr("scale"));
    sbA->getSpinBox()->setValue(scale());
    sbA->getSpinBox()->setSingleStep(0.1);
    connect(sbA->getSpinBox(),SIGNAL(valueChanged(double)),this,SLOT(scaleChanged(double)));

    SPinBoxAction * sbArotate = new SPinBoxAction(tr("rotate"));
    sbArotate->getSpinBox()->setValue(rotation());
    connect(sbArotate->getSpinBox(),SIGNAL(valueChanged(double)),this,SLOT(rotateChanged(double)));
    sbArotate->getSpinBox()->setMinimum(-360);
    sbArotate->getSpinBox()->setMaximum(360);

    CheckboxAction * scaleWithZoomAction = new CheckboxAction(tr("scale with zoom"));
    scaleWithZoomAction->getCheckBox()->setChecked(this->data(1).toBool());
    connect(scaleWithZoomAction->getCheckBox(),SIGNAL(toggled(bool)),this,SLOT(scaleWithZoom(bool)));

    QAction * backAction = new QAction(tr("send backward"),this);
    connect(backAction,SIGNAL(triggered()),this,SLOT(sendBackward()));

    QAction * foreAction = new QAction(tr("bring foreward"),this);
    connect(foreAction,SIGNAL(triggered()),this,SLOT(bringForeward()));


    menu.addAction(sA);
    menu.addAction(sbA);
    menu.addAction(sbArotate);
    menu.addAction(scaleWithZoomAction);
    menu.addAction(foreAction);
    menu.addAction(backAction);
    menu.addAction(closeAction);
    menu.exec(event->screenPos());
    menu.deleteLater();
}

void GraphicsFeatureItemSVG::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    if (theDock->getMapView()->mapViewMode==MapView::SELECT_ITEMS){
        this->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsFocusable);
        this->setFocus();
        theDock->getMapView()->itemWasSelected(this);
    } else {
        this->setFlag(QGraphicsItem::ItemIsMovable,false);
        this->setFlag(QGraphicsItem::ItemIsSelectable,false);
        this->setFlag(QGraphicsItem::ItemIsFocusable,false);
    }

    QGraphicsItem::mousePressEvent(event);
}

void GraphicsFeatureItemSVG::keyPressEvent(QKeyEvent *event)
{

    switch (event->key()) {
    case Qt::Key_Delete:
        this->deleteItem();
        break;
    default:
        break;
    }

    QGraphicsSvgItem::keyPressEvent(event);
}


QVariant GraphicsFeatureItemSVG::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
{

    QGraphicsItem::itemChange(change,value);

    return value;
}

void GraphicsFeatureItemSVG::mapHasUpdated(QPointF lonlat, int zoom, int viewScale)
{
    Q_UNUSED(lonlat)
    Q_UNUSED(viewScale)
    Q_UNUSED(zoom)
}

void GraphicsFeatureItemSVG::zoomHasChanged(int zoom_)
{
    if (zoom_==zoom) return;
    lastPixels = this->mapToScene(this->boundingRect().center());

    if (zoom_>zoom){
        this->setPos(lastPixels*2-this->boundingRect().center());
        if (this->data(1).toBool()){
            this->setTransformOriginPoint(this->boundingRect().center());
            this->setScale(this->scale()*2);
        }
    } else {
        this->setPos(lastPixels/2-this->boundingRect().center());
        if (this->data(1).toBool()){
            this->setTransformOriginPoint(this->boundingRect().center());
            this->setScale(this->scale()/2);
        }
    }

    zoom = zoom_;

}

void GraphicsFeatureItemSVG::deleteItem()
{
   // listItems->removeOne(this);
    this->deleteLater();
}

void GraphicsFeatureItemSVG::transChanged(int value)
{
    this->setOpacity(double(value)/100.0);
}

void GraphicsFeatureItemSVG::scaleChanged(double value)
{
    setTransformOriginPoint(this->boundingRect().center());
    this->setScale(double(value));
}

void GraphicsFeatureItemSVG::rotateChanged(double value)
{
    setTransformOriginPoint(this->boundingRect().center());
    this->setRotation(value);
}

void GraphicsFeatureItemSVG::sendBackward()
{
     this->setZValue(this->zValue()-1);
}

void GraphicsFeatureItemSVG::bringForeward()
{
    this->setZValue(this->zValue()+1);
}

void GraphicsFeatureItemSVG::scaleWithZoom(bool checked)
{
    qDebug()<<checked;
    this->setData(1,checked);
}
